package com.cash.app.ui.other

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.cash.app.R
import com.cash.app.base.BaseActivity
import com.cash.app.custom.AppActionBar
import com.cash.app.databinding.AcFeedbackBinding
import com.cash.app.ui.other.adapter.FeedbackListAdapter
import com.cash.app.utils.addVerticalSpacing

class FeedbackActivity : BaseActivity() {

    private lateinit var binding: AcFeedbackBinding
    private val viewModel: FeedbackACViewModel by viewModels()
    private val adapter = FeedbackListAdapter()

    override fun setViewBind() {
        binding = AcFeedbackBinding.inflate(layoutInflater)
    }

    override fun getLayoutView(): View {
        return binding.root
    }

    override fun getAppActionBar(): AppActionBar? {
        return binding.actionBar
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setupRecyclerView()
        setupObservers()
        setupListeners()
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }

    private fun setupRecyclerView() {
        binding.feedbackListRv.apply {
            layoutManager = LinearLayoutManager(this@FeedbackActivity)
            adapter = this@FeedbackActivity.adapter
            addVerticalSpacing(resources.getDimensionPixelSize(R.dimen.home_list_item_spacing))
        }
    }

    private fun setupObservers() {
        viewModel.feedbackList.observe(this) { response ->
            adapter.submitList(response.list)
            updateEmptyState(response.list.isEmpty())
        }
    }

    private fun setupListeners() {
        binding.btnAddFeedback.setOnClickListener {
            val intent = Intent(this, FeedbackSubmitActivity::class.java)
            startActivity(intent)
        }
    }

    private fun loadData() {
        viewModel.getFeedbackList()
    }

    private fun updateEmptyState(isEmpty: Boolean) {
        binding.feedbackListRv.isVisible = !isEmpty
        binding.ivEmpty.isVisible = isEmpty
    }
}